from bs4 import BeautifulSoup

# 这里放置你提供的 HTML 代码字符串
html_doc = """
<html lang="en">

<div class="city-container" data-val="{currentcityid:89 }">
    <div class="city-selected">
        <div class="city-name">
            常州
            <span class="caret"></span>
        </div>
    </div>
    <div class="city-list" data-val="{ localcityid: 10 }">
        <div class="city-list-header">定位城市：<a class="js-geo-city" data-ci="10">上海</a></div>

        <ul>
            <li>
                <span>A</span>
                <div>
                    <a class="js-city-name" data-ci="150" data-val="{ choosecityid:150 }" data-act="cityChange-click">阿拉善盟</a>
                    <a class="js-city-name" data-ci="1245" data-val="{ choosecityid:1245 }" data-act="cityChange-click">安陆市</a>
                    <a class="js-city-name" data-ci="1212" data-val="{ choosecityid:1212 }" data-act="cityChange-click">安州区</a>
                </div>
            </li>

            <li>
                <span>Z</span>
                <div>
                    <a class="js-city-name" data-ci="73" data-val="{ choosecityid:73 }" data-act="cityChange-click">郑州</a>
                    <a class="js-city-name" data-ci="81" data-val="{ choosecityid:81 }" data-act="cityChange-click">淄博</a>
                    <a class="js-city-name" data-ci="108" data-val="{ choosecityid:108 }" data-act="cityChange-click">珠海</a>
                </div>
            </li>

        </ul>
    </div>
</div>
</html>
"""

# 使用BeautifulSoup解析HTML
soup = BeautifulSoup(html_doc, 'html.parser')

# 找到所有城市名字的链接
city_links = soup.select('.js-city-name')  # 使用CSS选择器定位到所有的.js-city-name类的a标签

cities = [link.text for link in city_links]  # 提取每个链接中的文本（即城市名）
print("-----------")
# 打印城市列表
for city in cities:
    print(city)

print("-----------")
# 找到所有城市名字的链接并提取城市名及choosecityid
cities_with_id = [
    {'name': link.text, 'choosecityid': link['data-val'].split('choosecityid:')[1].split('}')[0]}
    for link in soup.select('.js-city-name')
]

# 打印城市名及对应的choosecityid
for city_info in cities_with_id:
    print(f"城市名称: {city_info['name']}, choosecityid: {city_info['choosecityid']}")

print("-----------")
# 找到所有城市名字的链接并提取城市名及data-ci作为ID
cities_with_id = [
    {'name': link.text, 'city_id': link['data-ci']}
    for link in soup.select('.js-city-name')
]


# 打印城市名及对应的ID
for city_info in cities_with_id:
    print(f"城市名称: {city_info['name']}, ID: {city_info['city_id']}")